<!DOCTYPE html>
<html>
<script>
    var old = console.log;
    console.log = function (message) {
        var logger = document.getElementById('log');
        if (typeof message == 'object') {
            logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify(message) : message) + '<br />';
        } else {
            logger.innerHTML += message + '<br />';
        }
    }
    function readFolderAndShow() {
        const folderPathEle = document.getElementsByName("folderPath")[0];
        // const outPutPathEle = document.getElementsByName("outPutPath")[0];
        // let outPutPath = outPutPathEle.value;
        const folderPath = folderPathEle.value;
        const extNameEle = document.getElementsByName("extName")[0];
        const extName = extNameEle.value;

        if (folderPath) {
            if (!extName) {
                alert("请输入文件名后缀");
                return;
            }
            window["extName"] = extName;
            window.readFolder(folderPath, extName, function (inputFilePaths) {
                // outPutPathEle.value = outPutPath;
                inputFilePaths.sort(function (a, b) {
                    const numA = parseInt(a.split("-")[1]);
                    const numB = parseInt(b.split("-")[1]);
                    return numA - numB;
                })
                const oldFileNameListShowEle = document.getElementsByName("oldFileNameListShow")[0];
                oldFileNameListShowEle.readonly = false;
                let text = "";
                inputFilePaths.forEach((value, index) => {
                    console.log(value)
                    text += value.split("\\").pop() + ((index < inputFilePaths.length - 1) ? "\n" : "");
                });
                oldFileNameListShowEle.value = text;

                oldFileNameListShowEle.readOnly = true;
                // window["outPutPath"] = outPutPath;
                window["inputFilePaths"] = inputFilePaths;
            });

        } else {
            alert("请输入文件夹路径");
        }
    }
    function renameFiles() {
        const newFileNamesTextAreaEle = document.getElementsByName("newFileNamesTextArea")[0];
        const folderPathEle = document.getElementsByName("folderPath")[0];
        const folderPath = folderPathEle.value;
        const outPutPathEle = document.getElementsByName("outPutPath")[0];
        let outPutPath = outPutPathEle.value;
        if (!outPutPath || outPutPath === "") {
            outPutPath = folderPath+"\\"+"output";
            outPutPathEle.value = outPutPath;
        }
        const newFileNamesStr = newFileNamesTextAreaEle.value;
        let newFileNames = [];
        if (newFileNamesStr) {
            newFileNames = newFileNamesStr.split("\n");
        }
        if (!newFileNames.length) {
            alert("新文件名列表为空");
            return;
        }
        const fileNameMap = {}
        let value;
        for (let i = 0; i < newFileNames.length; i++) {
            value = newFileNames[i];
            if (fileNameMap[value]) {
                alert(`文件名重复:${value}`);
                return;
            } else {
                fileNameMap[value] = 1;
            }
        }
        renameFileNames(inputFilePaths, outPutPath, extName, newFileNames);

    }
</script>

<body bgcolor="#ffffff">
    文件夹路径: <input type="text" name="folderPath" /><br>
    输出文件夹路径(默认是输入文件夹下的output):<input type="text" name="outPutPath" /><br>
    文件名前缀(比如:S28BW)<input type="text" name="fileNamePre" value="S28BW" />
    分隔符(比如: - 短线)<input type="text" name="splitChar" value="-" />
    后缀名: <input type="text" name="extName" value="pdf" /><br>
    旧的文件名列表:<textarea name="oldFileNameListShow" rows="20" cols="40"></textarea>
    新文件名列表:<textarea name="newFileNamesTextArea" rows="20" cols="40"></textarea><br>
    <button type="button" onclick="readFolderAndShow()">读取文件夹文件</button>
    <button type="button" onclick="renameFiles()">重命名</button>
    <div id="log"></div>
</body>

</html>